package cn.springmvc.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import cn.springmvc.common.datasource.DataSourceEnum;
import cn.springmvc.common.datasource.DynamicDataSourceHolder;
import cn.springmvc.entity.Permission;
import cn.springmvc.entity.User;
import cn.springmvc.repository.PermissionRepository;

/**
 * @author Vincent.wang
 *
 */
@Repository
public class PermissionDaoService {

    @Autowired
    protected PermissionRepository permissionRepository;

    public List<Permission> getPermissions(User user) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return permissionRepository.getPermissions(user);
    }

    public void addPermission(Permission permission) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        permissionRepository.addPermission(permission);
    }

    public Permission findPermissionByKey(String permissionKey) {
        DynamicDataSourceHolder.setDataSourceKey(DataSourceEnum.MASTER.getKey());
        return permissionRepository.findPermissionByKey(permissionKey);
    }

}
